package com.midea.ai.air.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.midea.ai.air.R;
import com.midea.ai.air.data.ResultModel;
import com.midea.ai.air.data.login.UserInfo;
import com.midea.ai.air.utils.HttpUtils;
import com.squareup.okhttp.RequestBody;

public class BaseFragment extends Fragment implements HttpUtils.callBack {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private FragmentTabManager mManager;
    private HttpUtils mHttp = new HttpUtils(this);


    private String mParam1;
    private String mParam2;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BaseFragment.
     */
    public static BaseFragment newInstance(String param1, String param2) {
        BaseFragment fragment = new BaseFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public BaseFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_base_fragmetn, container, false);
    }

    @Override
    public boolean get(String url) {
        return mHttp.get(url);
    }

    @Override
    public boolean post(String url, String json) {
        return mHttp.post(url, json);
    }

    @Override
    public boolean post(String url, RequestBody formBody) {
        return mHttp.post(url, formBody);
    }

    @Override
    public void onError(String url, Exception e) {
    }

    @Override
    public Object asyncExecute(String url, ResultModel resultModel) {
        return null;
    }

    @Override
    public void onFailure(String url, ResultModel resultModel) {
    }

    @Override
    public void onSucceed(String url, ResultModel resultModel) {
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public UserInfo getUserInfo() {
        return ((AirApplication) getActivity().getApplication()).getUserInfo();
    }

    /**
     * 获取用户Token
     *
     * @return
     */
    public String getToken() {
        if (getUserInfo() != null && getUserInfo().getToken() != null) {
            return getUserInfo().getToken();
        }
        return "";
    }

    /**
     * 获取用户id
     *
     * @return
     */
    public String getUid() {
        if (getUserInfo() != null && getUserInfo().getUid() != null) {
            return getUserInfo().getUid();
        }
        return "";
    }

    /**
     * 登出
     */
    public void logout() {
        ((AirApplication) getActivity().getApplication()).setUserInfo(null);
    }

    /**
     * 是否登录
     *
     * @return
     */
    public boolean isLogin() {
        return getUserInfo() != null && getUserInfo().getToken() != null;
    }

    /**
     * 无参数页面跳转
     *
     * @param cls
     */
    public void navigate(Class cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    public FragmentTabManager getManager() {
        return mManager;
    }

    public void setManager(FragmentTabManager mManager) {
        this.mManager = mManager;
    }
}
